package com.ruoyi.system.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.MiniDailyReplay;
import com.ruoyi.system.domain.MiniStockSubject;
import com.ruoyi.system.service.MiniStockSubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.MiniStockMapper;
import com.ruoyi.system.domain.MiniStock;
import com.ruoyi.system.service.IMiniStockService;

import static com.ruoyi.common.utils.PageUtils.startPage;

/**
 * 股票Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-11-07
 */
@Service
public class MiniStockServiceImpl extends ServiceImpl<MiniStockMapper, MiniStock> implements IMiniStockService 
{
    @Autowired
    private MiniStockSubjectService miniStockSubjectService;

    /**
     * 查询股票
     * 
     * @param id 股票主键
     * @return 股票
     */
    @Override
    public MiniStock selectMiniStockById(Long id)
    {
        MiniStock stock = this.getBaseMapper().selectMiniStockById(id);
        subjectIdStringToList(stock);
        return stock;
    }

    private static void subjectIdStringToList(MiniStock stock) {
        if (StringUtils.isNotEmpty(stock.getSubjectIds())){
            List<Long> subjectIdList = new ArrayList<>();
            for (String s : stock.getSubjectIds().split(",")) {
                subjectIdList.add(Long.valueOf(s));
            }
            stock.setSubjectIdList(subjectIdList);
        }
    }

    /**
     * 查询股票列表
     * 
     * @param miniStock 股票
     * @return 股票
     */
    @Override
    public List<MiniStock> selectMiniStockList(MiniStock miniStock)
    {
        if (miniStock.getSubjectId()!=null){
            List<Long> stockIds = miniStockSubjectService.selectStockBySubjectAncestors(miniStock.getSubjectId())
                    .stream().map(m->m.getStockId()).distinct().collect(Collectors.toList());
            miniStock.setStockIds(stockIds);
        }

        startPage();
        List<MiniStock> stocks = this.getBaseMapper().selectMiniStockList(miniStock);
        for (MiniStock stock : stocks) {
            subjectIdStringToList(stock);
        }
        return stocks;
    }

    @Override
    public List<MiniStock> selectMiniStockListNotGroup(MiniStock miniStock) {
        return this.getBaseMapper().selectMiniStockListNotGroup(miniStock);
    }

    /**
     * 新增股票
     *
     * @param miniStock 股票
     * @return 结果
     */
    @Override
    public AjaxResult insertMiniStock(MiniStock miniStock)
    {
        MiniStock repeatCheck = new MiniStock();
        repeatCheck.setStockNo(miniStock.getStockNo());
        List<MiniStock> list = this.selectMiniStockList(repeatCheck);
        if (CollUtil.isNotEmpty(list)){
            return AjaxResult.error("股票已存在，请修改原有股票或者删除后再新增");
        }
        miniStock.setCreateTime(DateUtils.getNowDate());
        int row = this.getBaseMapper().insertMiniStock(miniStock);
        insertStockSubjectRelation(miniStock);
        return AjaxResult.success(row);
    }

    private void insertStockSubjectRelation(MiniStock miniStock) {
        if (CollUtil.isEmpty(miniStock.getSubjectIdList())){
            return;
        }
        List<MiniStockSubject> insertList = new ArrayList<>();
        for (Long subjectId : miniStock.getSubjectIdList()) {
            insertList.add(new MiniStockSubject(miniStock.getId(), miniStock.getStockNo(), miniStock.getName(),subjectId));
        }
        miniStockSubjectService.saveBatch(insertList);
    }

    /**
     * 修改股票
     * 
     * @param miniStock 股票
     * @return 结果
     */
    @Override
    public int updateMiniStock(MiniStock miniStock)
    {
        miniStock.setUpdateTime(DateUtils.getNowDate());
        if (CollUtil.isNotEmpty(miniStock.getSubjectIdList())) {
            miniStockSubjectService.remove(Wrappers.<MiniStockSubject>lambdaQuery().eq(MiniStockSubject::getStockId,miniStock.getId()));
            insertStockSubjectRelation(miniStock);
        }
        return this.getBaseMapper().updateMiniStock(miniStock);
    }

    /**
     * 批量删除股票
     * 
     * @param ids 需要删除的股票主键
     * @return 结果
     */
    @Override
    public int deleteMiniStockByIds(Long[] ids)
    {
        return this.getBaseMapper().deleteMiniStockByIds(ids);
    }

    /**
     * 删除股票信息
     * 
     * @param id 股票主键
     * @return 结果
     */
    @Override
    public int deleteMiniStockById(Long id)
    {
        return this.getBaseMapper().deleteMiniStockById(id);
    }

    /**
     * 批量更新涨幅
     *
     * @param updateStockList
     */
    @Override
    public void batchUpdateRise(List<MiniStock> updateStockList) {
        this.getBaseMapper().batchUpdateRise(updateStockList);
    }

    /**
     * 根据题材id列表查股票
     *
     * @param subjectIds
     * @return
     */
    @Override
    public List<MiniStock> getMiniStockListBySubjectIds(List<Long> subjectIds) {
        return this.getBaseMapper().getMiniStockListBySubjectIds(subjectIds);
    }

    /**
     * 根据题材id列表查股票
     * @param day
     * @return
     */
    @Override
    public List<MiniStock> selectMiniStockByDay(String day) {
        return this.getBaseMapper().selectMiniStockByDay(day);
    }

    /**
     * 根据题材id查下面的股票
     *
     * @param subjectId
     * @return
     */
    @Override
    public List<MiniStock> selectStockBySubject(Long subjectId) {
        return this.getBaseMapper().selectStockBySubject(subjectId);
    }

    @Override
    public Map<String, Object> selectableStock(Long subjectId) {
        Map<String, Object> result = new HashMap<>(2);
        List<Long> selectedStockIds = new ArrayList<>();
        List<MiniStockSubject> list = miniStockSubjectService.list(Wrappers.<MiniStockSubject>lambdaQuery().eq(MiniStockSubject::getSubjectId, subjectId));
        if (CollUtil.isNotEmpty(list)){
            selectedStockIds.addAll(list.stream().map(MiniStockSubject::getStockId).collect(Collectors.toList()));
        }
        List<MiniStock> stockList = this.list(Wrappers.<MiniStock>lambdaQuery().eq(MiniStock::getDeleted, 0));
        result.put("selectedStockIds",selectedStockIds);
        result.put("selectableStocks",stockList);
        return result;
    }
}
